Ext.define('Holo.controller.ManagerTrip', {
    extend: 'Ext.app.Controller',
	
    views: [
        'ManagerTrip',
        'trip.FormCreateTripDesc',
        'trip.ListTripDesc',
        'trip.SelectTripSpots'
    ],
    
    stores: [
        'Trips',
        'TripSpots'
    ],
    
    
    refs: [
	{
	   selector: 'createtripdesc',
	   ref: 'formAddLabel'
	},
        {
	   selector: 'selecttripspots',
	   ref: 'formTripSpots'
	}
    ],


    
    init: function() {
    	 this.control({
        
            'listtripdesc': {
                afterrender : function(cmp){
		    //load the store ('TripDescriptions') affected when we create the cmp
                    cmp.getStore().load();
                }
            },
            
            'createtripdesc button[action=createtripdesc]': {
                click : this.createTripDescription
            
            },
            
            
            'selecttripspots itemselector' : {
                afterrender : function(cmp){
                    //affect the spot selected of the current trip
                    cmp.setValue(this.getTripSpotsStore().arrayOfid());
                    //this.getTripSpotsStore().load();
                }
            },
            
            'selecttripspots button[action=savetripspots]':{
                click : this.saveTripSpotsLink
            }
            
        
        });
    },
    

    /**
     * Submit the forme. Add a Trip description type Label to the TripDescriptions
     */
    createTripDescription : function(){
        var newDescriptionData = this.getFormAddLabel().getValues();
        try{
                var newTripDescription = Ext.create('Holo.model.TripDescription', {
                                            trip_id: GlobalData.currentTrip.get('id'),
                                            language: newDescriptionData.codelang,
                                            title: newDescriptionData.title,
                                            text: newDescriptionData.description
                                        });
                
               // console.log(newTripDescription);
		Ext.data.StoreManager.lookup('TripDescriptions').add(newTripDescription);
                this.getFormAddLabel().getForm().reset();
                return true;
                
        }catch(err){
                console.log('erreur : controller/ManagerTrip.createTripDescriptionTypeLabel() =>'+ err.message);
                return false;
        }
    },
    
    
    
    saveTripSpotsLink : function (){
        var formdata = this.getFormTripSpots().getValues();
        var tabSpotIdSelected = formdata.tripspotlink.split(',');
        var storeTripSpots = this.getTripSpotsStore();
        var currentTripSpotRelation;
        var order;
        
        storeTripSpots.removeOldInstance(tabSpotIdSelected);
        
        for (var i=0; i < tabSpotIdSelected.length ; i++) {
            order = i + 1;
            indexTripSpotToUpdate =storeTripSpots.findExact('spot_id', tabSpotIdSelected[i]);
            if(indexTripSpotToUpdate==-1){
                currentTripSpotRelation = Ext.create('Holo.model.TripSpot', {
                                              trip_id: GlobalData.currentTrip.get('id'),
                                              spot_id: tabSpotIdSelected[i] ,
                                              list_order: order,
                                              user_id :GlobalData.currentUserId
                                          });
                storeTripSpots.add(currentTripSpotRelation);
            }else{
                storeTripSpots.getAt(indexTripSpotToUpdate).set('list_order', order);  
            }
        }
       
        storeTripSpots.sync();
    }
    
 });